<script>
$(".cp_claim").click(function(e){
    e.preventDefault();
    e.stopImmediatePropagation();
    CLAIM_ID            = $(this).data('id');
    NEED_TO_COFIRM_LIKE = ($(this).data('onlyfan') == 1);
    FB.login(function(response) {
        if( response.status === 'unknown')
        {
            alert('<?php echo $this->translate('Please accept permission to get the coupon') ?>');
        }
        else if (response.status === 'connected' )
        {
            if (response.authResponse && response.authResponse.userID)
            {
                if( NEED_TO_COFIRM_LIKE )
                {
                    var user_id             = response.authResponse.userID;
                    var fql_query           = "SELECT uid FROM page_fan WHERE page_id = "+<?php echo $this->social->getIdPage()?>+" and uid = "+user_id;
                    var the_query           = FB.Data.query(fql_query);
                    var signed_request      = response.authResponse.signedRequest;

                    the_query.wait(function(rows) {
                        if (rows.length == 1 && rows[0].uid == user_id) {
                            $.post(
                                '<?php echo $this->baseUrl('/page/index/claim/id/') ?>'+CLAIM_ID,
                                {
                                    signed_request:signed_request
                                },
                                function(){
                                    parent.location.href = '<?php echo $this->social->getFanPageUrl($this->social->getIdPage(), 'undefined') ?>';
                                }
                            );
                        } else {
                            alert('<?php echo $this->translate('You must like this page to claim the coupon') ?>');
                        }
                    });
                }
                else
                {
                    $.post(
                        '<?php echo $this->baseUrl('/page/index/claim/id/') ?>'+CLAIM_ID,
                        {
                            signed_request:signed_request
                        },
                        function(){
                            parent.location.href = '<?php echo $this->social->getFanPageUrl($this->social->getIdPage(), 'undefined') ?>';
                        }
                    );
                }
            }
        }
    }, {scope:''});
    return false;
});
</script>